﻿// clear search results
function clearSearchMapResults() {
	$(".searchresultlist ul li").remove();

	// remove previus markers
	$(searchedMarkers).each(function() {
		map.removeOverlay(this.gMarker);
	});

	// clear array
	while (searchedMarkers.length > 0)
		searchedMarkers.pop();

	return false;
}

// start google map search
function startSearchMap() {
	var textToSearch = $("input#SearchText").val();
	if (textToSearch.length > 0) {
		// exec search
		geocoder.setViewport(map.getBounds());
		geocoder.getLocations(textToSearch, AddAddressToMap);

		// clear search results
		clearSearchMapResults();

		// activate tab
		activateTab($("#SearchMapTab"));
	}
}

// process found item cliced
function onFoundItemClicked(item) {
	var ll = new GLatLng($(item).attr('lat'), $(item).attr('lng'));
	map.panTo(ll);
	$(searchedMarkers).each(function() {
		if (this.LatLng.lat() == ll.lat() && this.LatLng.lng() == ll.lng()) {
			this.gMarker.openInfoWindowHtml(this.GetInfo());
		}
	});
	return false;
}

// addAddressToMap() is called when the geocoder returns an
// answer.  It adds a marker to the map with an open info window
// showing the nicely formatted version of the address and the country code.
function AddAddressToMap(response) {
	if (!response || response.Status.code != 200) {
		alert("Sorry, we were unable to geocode that address");
	}
	else {
		// process result
		var latlngbounds = new GLatLngBounds();
		var itemNumber = 1;
		$(response.Placemark).each(function() {
			place = this;
			point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
			latlngbounds.extend(point);
			coords = point.toUrlValue();
			var itemNumberName = "<b>" + itemNumber + ". </b>" + response.name;

			$(".searchresultlist ul").append(
				$("<li>")
				.append(
					$("<a>")
					.attr('lat', point.lat())
					.attr('lng', point.lng())
					.attr('href', '')
					.addClass('founditem')
					.html(itemNumberName + '<br/>' + this.address + '<br/>' + coords)
				)
			);

			options = { title: response.name, clickable: true };
			var marker = new GMarker(point, options);
			var fm = new FoundMarker(itemNumberName, place.address, marker, point);
			searchedMarkers.push(fm);
			GEvent.addListener(marker, "click", function() {
				fm.gMarker.openInfoWindowHtml(fm.GetInfo());
				//fm.ShowInfo map.removeOverlay(marker) 
			});
			//GEvent.addListener(marker, "click", function() { map.removeOverlay(marker) });
			//marker.openInfoWindowHtml(response.name + '<br>' + place.address + '<br>' + coords);

			// set smart zoom
			var zoomLevel = map.getBoundsZoomLevel(latlngbounds);
			if (zoomLevel > 11)
				zoomLevel = 11;
			// move map
			map.setCenter(latlngbounds.getCenter(), zoomLevel);
			itemNumber++;
		});

		// attach click handle
		$(".searchresultlist ul li a").live("click", function() {
			return onFoundItemClicked(this);
		});


		// show markers
		$(searchedMarkers).each(function() {
			map.addOverlay(this.gMarker);
		});


		//		place = response.Placemark[0];
		//		point = new GLatLng(place.Point.coordinates[1],
		//                            place.Point.coordinates[0]);
		//		options = { title: response.name, clickable: true };
		//		var marker = new GMarker(point, options);
		//		map.addOverlay(marker);
		//		GEvent.addListener(marker, "click", function() { map.removeOverlay(marker) });
		//		coords = point.toUrlValue();
		//		marker.openInfoWindowHtml(response.name + '<br>' + place.address + '<br>' + coords);

		//		map.setCenter(point, 11);
	}
}
